﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    
    class Program
    {
        static void Main(string[] args)
        {
            Fight f = new Fight();
            f.Begin();
        }
    }

    class Fight {
        private string name;
        private int ran;

        public string Name { get => name; set => name = value; }
        public int Ran { get => ran; set => ran = value; }

        public Fight() { }
        public Fight(string name, int ran) {
            this.name = name;
            this.ran = ran;
        }

        public void Begin() {
            Console.WriteLine("*****************************");
            Console.WriteLine("*********猜拳游戏************");
            Console.WriteLine("*****************************");
            Console.WriteLine("出拳规则，石头剪刀布");
            Console.WriteLine("请输入玩家姓名:");
            string str = Console.ReadLine();
            RandomGamer(str);
        }

        public void RandomGamer(string gamer) {

            string[] name = new string[] { "刘备", "关羽", "张飞", "刘禅", "秦始皇", "唐玄宗", "唐三藏", "孙悟空", "猪八戒", "沙僧", "李小龙", "周星驰", "叶辰", "王昊", "王维", "李白", "张三", "司马迁", "司马懿", "荀子", "老子", "阿基米德", "爱因斯坦", "牛顿", "夏洛特" };
            string[] arr = new string[3]; 
            for (int i = 0; i < 3; i++) {
                Random ran = new Random();
                int num = ran.Next(0, 26);
                if (!arr.Contains(name[num]))
                {
                    arr[i] = name[num];
                }
                else {
                    i--;
                }
                
            }
            Console.WriteLine($"玩家:{gamer},角色1:{arr[0]},角色2:{arr[1]},角色3:{arr[2]}");
            a: Console.WriteLine("是否开始游戏[Y/N]");
            string str = Console.ReadLine();
            switch (str) {
                case "Y":
                    RandomFight(arr,gamer);
                    break;
                case "N":
                    Begin();
                    break;
                default:
                    Console.WriteLine("输入错误");
                    goto a;
            }
        }

        public void RandomFight(string[] newName, string gamer) {

            int count = 1;
            int[] arr = new int[3];
        a: Console.WriteLine($"Round {count}");

            for (int i = 0; i < arr.Length; i++)
            {
                Random ran = new Random();
                arr[i] = ran.Next(1, 4);
            }

            arr[0] = 1;
            arr[1] = 3;
            arr[2] = 3;


            int num = FightWhat();

            if (num == 1)
            {
                if ((arr[0] == 2 && arr[1] == 1 && arr[2] == 1) || (arr[0] == 1 && arr[1] == 1 && arr[2] == 2) || (arr[0] == 1 && arr[1] == 2 && arr[2] == 1))
                {
                    for (int i = 0; i < arr.Length; i++)
                    {
                        if (arr[i] == 2)
                        {
                            Console.WriteLine($"出牌者 {newName[i]}赢了，{gamer} 为最后一名");
                        }
                    }

                }
                else if (arr[0] == 3 && arr[1] == 3 && arr[2] == 3)
                {
                    Console.WriteLine($"玩家 {gamer}赢了，通吃！");
                }
                else if ((arr[0] == 1 && arr[1] == 3 && arr[2] == 3) || (arr[0] == 3 && arr[1] == 1 && arr[2] == 3) || (arr[0] == 3 && arr[1] == 3 && arr[2] == 1))
                {
                    for (int i = 0; i < arr.Length; i++)
                    {
                        if (arr[i] == 1)
                        {
                            Console.WriteLine($"玩家 {gamer}和出牌者 {newName[i]}赢了,进入下一轮厮杀");
                            string[] name = new string[] { newName[i] };
                            NextFight(count, gamer, name);

                        }
                    }
                }
                else if ((arr[0] == 3 && arr[1] == 3 && arr[2] == 1) || (arr[0] == 1 && arr[1] == 3 && arr[2] == 3) || (arr[0] == 3 && arr[1] == 1 && arr[2] == 3))
                {
                    string[] name = new string[2];
                    for (int i = 0; i < arr.Length; i++)
                    {
                        if (arr[i] == 3)
                        {
                            int count2 = 1;

                            if (count2 == 1) { name[0] = newName[i]; }
                            else
                            {
                                name[1] = newName[i];
                            }
                            count++;

                        }
                    }
                    Console.WriteLine($"玩家 {gamer}和出牌者 {name[0]},{name[1]}赢了,进入下一轮厮杀");
                    NextFight(count, gamer, name);
                }
                else
                {
                    Console.WriteLine($"谁都没落着好，下一把");
                    count++;
                    goto a;
                }

            }
            else if (num == 2)
            {
                if ((arr[0] == 3 && arr[1] == 2 && arr[2] == 2) || (arr[0] == 2 && arr[1] == 2 && arr[2] == 3) || (arr[0] == 2 && arr[1] == 3 && arr[2] == 2))
                {
                    for (int i = 0; i < arr.Length; i++)
                    {
                        if (arr[i] == 3)
                        {
                            Console.WriteLine($"出牌者 {newName[i]}赢了，{gamer} 为最后一名");
                        }
                    }

                }
                else if (arr[0] == 1 && arr[1] == 1 && arr[2] == 1)
                {
                    Console.WriteLine($"玩家 {gamer}赢了，通吃！");
                }
                else if ((arr[0] == 2 && arr[1] == 1 && arr[2] == 1) || (arr[0] == 1 && arr[1] == 2 && arr[2] == 1) || (arr[0] == 1 && arr[1] == 1 && arr[2] == 2))
                {
                    for (int i = 0; i < arr.Length; i++)
                    {
                        if (arr[i] == 2)
                        {
                            Console.WriteLine($"玩家 {gamer}和出牌者 {newName[i]}赢了,进入下一轮厮杀");
                            string[] name = new string[] { newName[i] };
                            NextFight(count, gamer, name);

                        }
                    }
                }
                else if ((arr[0] == 2 && arr[1] == 2 && arr[2] == 1) || (arr[0] == 1 && arr[1] == 2 && arr[2] == 2) || (arr[0] == 2 && arr[1] == 1 && arr[2] == 2))
                {
                    string[] name = new string[2];
                    for (int i = 0; i < arr.Length; i++)
                    {
                        if (arr[i] == 2)
                        {
                            int count2 = 1;

                            if (count2 == 1) { name[0] = newName[i]; }
                            else
                            {
                                name[1] = newName[i];
                            }
                            count++;

                        }
                    }
                    Console.WriteLine($"玩家 {gamer}和出牌者 {name[0]}, {name[1]}赢了,进入下一轮厮杀");
                    NextFight(count, gamer, name);
                }
                else
                {
                    Console.WriteLine($"谁都没落着好，下一把");
                    count++;
                    goto a;
                }
            }
            else {
                if ((arr[0] == 1 && arr[1] == 3 && arr[2] == 3) || (arr[0] == 3 && arr[1] == 3 && arr[2] == 1) || (arr[0] == 3 && arr[1] == 1 && arr[2] == 3))
                {
                    for (int i = 0; i < arr.Length; i++)
                    {
                        if (arr[i] == 2)
                        {
                            Console.WriteLine($"出牌者 {newName[i]}赢了，{gamer} 为最后一名");
                        }
                    }

                }
                else if (arr[0] == 2 && arr[1] == 2 && arr[2] == 2)
                {
                    Console.WriteLine($"玩家 {gamer}赢了，通吃！");
                }
                else if ((arr[0] == 2 && arr[1] == 3 && arr[2] == 2) || (arr[0] == 3 && arr[1] == 2 && arr[2] == 2) || (arr[0] == 2 && arr[1] == 2 && arr[2] == 3))
                {
                    for (int i = 0; i < arr.Length; i++)
                    {
                        if (arr[i] == 3)
                        {
                            Console.WriteLine($"玩家 {gamer}和出牌者 {newName[i]}赢了,进入下一轮厮杀");
                            string[] name = new string[] { newName[i] };
                            NextFight(count, gamer, name);

                        }
                    }
                }
                else if ((arr[0] == 3 && arr[1] == 3 && arr[2] == 2) || (arr[0] == 2 && arr[1] == 3 && arr[2] == 3) || (arr[0] == 3 && arr[1] == 2 && arr[2] == 3))
                {
                    string[] name = new string[2];
                    for (int i = 0; i < arr.Length; i++)
                    {
                        if (arr[i] == 3)
                        {
                            int count2 = 1;

                            if (count2 == 1) { name[0] = newName[i]; }
                            else
                            {
                                name[1] = newName[i];
                            }
                            count++;

                        }
                    }
                    Console.WriteLine($"玩家 {gamer}和出牌者 {name[0]},{name[1]}赢了,进入下一轮厮杀");
                    NextFight(count, gamer, name);
                }
                else
                {
                    Console.WriteLine($"谁都没落着好，下一把");
                    count++;
                    goto a;
                }

            }
 
        }

        public int FightWhat() {

            Console.WriteLine("请输入你要出：1、剪刀,2、石头,3、布");
            int num;
        b: try
            {
                num = int.Parse(Console.ReadLine());
            }
            catch (Exception)
            {
                Console.WriteLine("输入错误");
                goto b;
            }

            return num;
        }

        private void NextFight(int count, string gamer, string[] name)
        {
            if (name.Length == 1)
            {
                c: int num = FightWhat();
                Console.WriteLine($"Round {count}");

                int ran = new Random().Next(1, 4);
                if (num == 1)
                {
                    if (ran == 2)
                    {
                        Console.WriteLine($"出牌者 {name[0]} 赢了!");
                    }
                    else if (ran == 3)
                    {
                        Console.WriteLine($"玩家 {gamer} 获得了最终的胜利！");
                    }
                    else
                    {
                        Console.WriteLine("再来一把！快要分胜负了！");
                        count++;
                        goto c;
                    }
                }
                else if (num == 2)
                {
                    if (ran == 1)
                    {
                        Console.WriteLine($"玩家 {gamer} 获得了最终的胜利！");
                    }
                    else if (ran == 3)
                    {
                        Console.WriteLine($"出牌者 {name[0]} 赢了!");
                    }
                    else
                    {
                        Console.WriteLine("再来一把！快要分胜负了！");
                        count++;
                        goto c;
                    }
                }
                else
                {
                    if (ran == 1)
                    {
                        Console.WriteLine($"出牌者 {name[0]} 赢了!");
                    }
                    else if (ran == 2)
                    {
                        Console.WriteLine($"玩家 {gamer} 获得了最终的胜利！");
                    }
                    else
                    {
                        Console.WriteLine("再来一把！快要分胜负了！");
                        count++;
                        goto c;
                    }
                }


            }
            else {
                int[] arr = new int[2];
                d: int num = FightWhat();
                Console.WriteLine($"Round {count}");
                for (int i = 0; i < arr.Length; i++)
                {
                    int ran = new Random().Next(1, 4);
                    arr[i] = ran;
                }

                if (num == 1)
                {
                    if ((arr[0] == 1 && arr[1] == 3) || (arr[0] == 3 && arr[1] == 1))
                    {
                        for (int i = 0; i < arr.Length; i++)
                        {
                            if (arr[i] == 1)
                            {
                                Console.WriteLine($"玩家 {gamer}和出牌者 {name[i]}进入总决赛");
                                string[] name2 = new string[] { name[i] };
                                count++;
                                NextFight(count, gamer, name2);
                            }
                        }

                    }
                    else if ((arr[0] == 1 && arr[1] == 2) || (arr[0] == 2 && arr[1] == 1))
                    {
                        for (int i = 0; i < arr.Length; i++)
                        {
                            if (arr[i] == 2)
                            {
                                Console.WriteLine($"出牌者{name[i]}获得了最终胜利");
                            }
                        }
                    }
                    else if (arr[0] == 3 && arr[1] == 3)
                    {
                        Console.WriteLine($"玩家{gamer}赢得最终胜利");
                    }
                    else
                    {
                        Console.WriteLine("再来一轮");
                        count++;
                        goto d;
                    }
                }
                else if (num == 2)
                {

                    if ((arr[0] == 2 && arr[1] == 1) || (arr[0] == 1 && arr[1] == 2))
                    {
                        for (int i = 0; i < arr.Length; i++)
                        {
                            if (arr[i] == 2)
                            {
                                Console.WriteLine($"玩家 {gamer}和出牌者 {name[i]}进入总决赛");
                                string[] name2 = new string[] { name[i] };
                                count++;
                                NextFight(count, gamer, name2);
                            }
                        }

                    }
                    else if ((arr[0] == 2 && arr[1] == 3) || (arr[0] == 3 && arr[1] == 2))
                    {
                        for (int i = 0; i < arr.Length; i++)
                        {
                            if (arr[i] == 3)
                            {
                                Console.WriteLine($"出牌者{name[i]}获得了最终胜利");
                            }
                        }
                    }
                    else if (arr[0] == 1 && arr[1] == 1)
                    {
                        Console.WriteLine($"玩家{gamer}赢得最终胜利");
                    }
                    else
                    {
                        Console.WriteLine("再来一轮");
                        count++;
                        goto d;
                    }

                }
                else {
                    if ((arr[0] == 3 && arr[1] == 2) || (arr[0] == 2 && arr[1] == 3))
                    {
                        for (int i = 0; i < arr.Length; i++)
                        {
                            if (arr[i] == 3)
                            {
                                Console.WriteLine($"玩家 {gamer}和出牌者 {name[i]}进入总决赛");
                                string[] name2 = new string[] { name[i] };
                                count++;
                                NextFight(count, gamer, name2);
                            }
                        }

                    }
                    else if ((arr[0] == 1 && arr[1] == 3) || (arr[0] == 3 && arr[1] == 1))
                    {
                        for (int i = 0; i < arr.Length; i++)
                        {
                            if (arr[i] == 1)
                            {
                                Console.WriteLine($"出牌者{name[i]}获得了最终胜利");
                            }
                        }
                    }
                    else if (arr[0] == 2 && arr[1] == 2)
                    {
                        Console.WriteLine($"玩家{gamer}赢得最终胜利");
                    }
                    else
                    {
                        Console.WriteLine("再来一轮");
                        count++;
                        goto d;
                    }
                }
                
            }
        }
    }
}
